package sctu.edu.java.year2016.team04.luoxinghua.Projectmain;


import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 落叶的思恋 on 2016/6/21.
 */
public class Main {
    Scanner scanner = new Scanner(System.in);
    List<String> stringList = new ArrayList<>();
    /*人工服务*/
    static String Engineeranswer;
    static String Userproblem;
    static String SavetheUserOptions;
    static String selectwork;
    static String userneedoperate;
    String password;
    static int content;
    static String YN;
    static String userYN;
    static String add;
    String leaveword;
    static String returnlayer;
    static int numberengineer;
    /*文件创建*/
    File fileanswer = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase");/*答案库的创建*/
    File file = new File("C:\\", "TheFourthGroupFile");
    File file0 = new File("C:\\TheFourthGroupFile", "QuestionAnswerDatabase");
    File filedir = new File("C:\\TheFourthGroupFile", "ManualService ");
    File fileco = new File("C:\\TheFourthGroupFile", "ContactInformation");
    String str;
    static String sss;

    /* 定义答案和和新建文件*/
    protected void probtype() throws IOException {
        file.mkdir();
        fileco.mkdir();
        file0.mkdir();
        filedir.mkdir();
        /*创建文件蓝屏*/
        File file1 = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", "蓝屏.txt");
        try {
            file1.createNewFile();
            FileOutputStream fileOutputStream = new FileOutputStream(file1);
            str = "蓝屏一般为CPU太热,灰尘,病毒,硬盘有坏道,内存质量问题 在开始菜单运行中输入msconfig回车,在启动里将所有(除ctfmon)对勾都去掉保存重启动试试(可以加快启动速度)," +
                    "一般是你启动运行程度太多,或文件冲突问题 安装360安全卫士,开机一直按F8进入安全模式查杀,现在网上十个毒有九个是木马,所以杀毒软件都查不到毒,建议安装360查杀 散热不好," +
                    "重新安装CPU散热片,可能接触不良,可更换CPU风扇 更换内存,或把内存上的灰尘清一清 如果发现有硬盘撞击声,后蓝屏,说明硬盘有坏道);";
            byte[] bytes = new byte[100000000];
            bytes = str.getBytes();
            fileOutputStream.write(bytes);
        } catch (IOException e) {
            e.getMessage();
        }
        try {
            str = ",  死机一般是因为运行软件过多,或电脑散热不好,太热,更换CPU风扇\n" +
                    "在开始菜单运行中输入msconfig回车,在启动里将所有(除ctfmon)\n" +
                    " 对勾都去掉保存重启动试试(可以加快启动速度),一般是你启动运行" +
                    "程度太多,或文件冲突问题散热不好,重新安装CPU散热片,可以接触不良,可更换CPU风扇";
            File file2 = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", "死机.txt");
            file2.createNewFile();
            FileOutputStream fileOutputStream2 = new FileOutputStream(file2);
            byte[] bytes2 = new byte[100000000];
            bytes2 = str.getBytes();
            fileOutputStream2.write(bytes2);
        } catch (IOException e) {
            e.getMessage();
        }
        str = "运行速度慢在开始菜单运行中输入msconfig回车,在启动里将所有(除ctfmon)对勾都去掉保存重启动试试(可以加快启动速度),一般是你启动运行程度太多,或文件冲突问题" +
                "安装360安全卫士,开机一直按F8进入安全模式查杀,现在网上十个毒有九个是木马,所以杀毒软件都查不到毒,建议安装360查杀";

        File file3 = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", "运行速度慢.txt");
        file3.createNewFile();
        FileOutputStream fileOutputStream3 = new FileOutputStream(file3);
        byte[] bytes3 = new byte[100000000];
        bytes3 = str.getBytes();
        fileOutputStream3.write(bytes3);
        str = "修改工程师联系方式电脑黑屏,无法开机为内存或显卡接触不良或内存质量问量或电源问题(无声或和嘀嘀声)\n" +
                " 一般为重新插拔显卡和内存卡,最主要的是清理CPU和内存附近的灰尘,最容易这样\n" +
                " 或更换内存或显卡或电源试试";
        File file4 = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", "黑屏.txt");
        try {
            file4.createNewFile();
            FileOutputStream fileOutputStream4 = new FileOutputStream(file4);
            byte[] bytes4 = new byte[100000000];
            bytes4 = str.getBytes();
            fileOutputStream4.write(bytes4);
        } catch (Exception e) {
            e.getMessage();
        }
        str = "网速慢,如果你的网速太慢，不妨先登录诸如Speedtest.net这样的测速网站来测试一下网络速度，\n" +
                "如果速度的确缓慢，再检查一下路由器， 如果都正常的话，就可以带电话给宽带供应商咨询一下是否网络服务出现问题。\n" +
                "如果始终无法连接有线或是WIFI网络，则可能是网卡硬件出现问题，先尝试下载安装最新驱动，如果仍然无效，则有可能是硬件问题，就需要更换了。";
        File file5 = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", "网速慢.txt");
        try {
            file5.getParent();
            FileOutputStream fileOutputStream5 = new FileOutputStream(file5);
            byte[] bytes5 = new byte[100000000];
            bytes5 = str.getBytes();
            fileOutputStream5.write(bytes5);
        } catch (Exception e) {
            e.getMessage();
        }

    }

    /*选择要进入的界面 如果进入工程师界面将会提示输入密码*/
    String ChooseId() {
        System.out.println("你要进入工程师页面还是客户页面（ Do you want to enter the page of the customer or engineer page）");
        SavetheUserOptions = scanner.nextLine();
        if (SavetheUserOptions.equals("工程师")) {
            System.out.println("请输入密码（please enter password）");
            password = scanner.nextLine();
        } else if (SavetheUserOptions.equals("客户") || SavetheUserOptions.equals("user")) {
            System.out.println("欢迎您的到来（Welcome your arrival）");
        } else {
            for (int i = 1; i > 0; ) {
                System.out.println("输入错误(enter error)");
                System.out.println("应该输入 “客户” ，“工程师” (should input“客户” ”工程师“ )");
                SavetheUserOptions = scanner.nextLine();
                if (SavetheUserOptions.equals("客户")) {
                    break;
                }
                if (SavetheUserOptions.equals("工程师")) {
                    System.out.println("请输入密码（please enter password）");
                    password = scanner.nextLine();
                    break;
                }
            }
        }
        return password;
    }

    /*卸载软件*/
    protected void Unload() {
        System.out.println("你是否要卸载软件（输入“是”“否”）");
        String decide = scanner.nextLine();
        while (true) {
            if (decide.equals("是")) {
                file.delete();
                break;
            } else if (decide.equals("否")) {
                System.out.println("谢谢您的信任");
                break;
            } else {
                System.out.println("输入错误请重新输入");
            }
        }
    }


    /*判断密码的正确性 正确将会进入并询问工程师要做什么 不然将会一直循环*/
    protected class Engineer {
        protected void engineerinterface() {
            if (password.equals("123456")) {
                System.out.println("欢迎回来工程师（Welcome back engineer）");
            } else {
                for (int i = 1; i > 0; ) {
                    System.out.println("请重新输入密码（please enter the password again）");
                    password = scanner.nextLine();
                    if (password.equals("123456")) {
                        System.out.println("欢迎回来工程师（Welcome back engineer）");
                        break;
                    }
                }
            }
        }

        /*工程师选择工作*/
        protected void decsiionWork() {
            System.out.println("你要“查看留言”还是需要“修改工程师联系方式”“添加内容”");
            selectwork = scanner.nextLine();
            if (selectwork.equals("查看留言")) {
                System.out.println("你将看到留言");
            } else if (selectwork.equals("修改工程师联系方式")) {
                System.out.println("你将修改工程师联系方式（you will make appointment engineer ）");
            } else if (selectwork.equals("添加内容")) {
                System.out.println("你将修改内容）");
            } else while (true) {
                System.out.println("输入错误");
                System.out.println("你要“查看留言”还是需要“修改工程师联系方式”“添加内容”");
                selectwork = scanner.nextLine();
                if ((selectwork.equals("修改工程师联系方式")) || (selectwork.equals("添加内容")) || selectwork.equals("查看留言"))
                    break;

            }
        }

        /* 修改工程师联系方式*/
        protected void userappointment() throws IOException {

            String[] contain = new String[]{
                    "张三 , 123456789,  147852963",
                    "李四 , 123456789,  741852963",
                    "王麻子 , 123456789,  785396214",
                    "赵钱 ,  123456789,  789545632"};
            File filecont = new File("C:\\TheFourthGroupFile\\ContactInformation\\1.txt");
            if (!filecont.exists()) {
                for (int i = 0; i < contain.length; i++) {
                    File fil = new File("C:\\TheFourthGroupFile\\ContactInformation", +(i + 1) + ".txt");
                    fil.createNewFile();
                    FileOutputStream fileOutputStream = new FileOutputStream(fil);
                    byte[] bytes4 = new byte[100000000];
                    bytes4 = contain[i].getBytes();
                    fileOutputStream.write(bytes4);
                }

            }
            File file = new File("C:\\TheFourthGroupFile\\ContactInformation");
            String[] s = file.list();
            System.out.println("姓名，  电话，      QQ");
            for (int i = 0; i < s.length; i++) {
                FileInputStream fileInputStream = new FileInputStream(new File("C:\\TheFourthGroupFile\\ContactInformation\\" + (i + 1) + ".txt"));
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String leave;
                while ((leave = reader.readLine()) != null) {
                    System.out.println(leave);
                }
            }
            System.out.println("你要“删除”还是“添加”的工程师联系");
            String choose = scanner.nextLine();
            if (choose.equals("添加")) {
                while (true) {/*开始添加*/
                    System.out.print("姓名：");
                    String name = scanner.nextLine();
                    System.out.print("电话：");
                    String num = scanner.nextLine();
                    System.out.print("QQ：");
                    String QQ = scanner.nextLine();
                    String temp = name + " , " + num + ",  " + QQ;
                    File filecontaintxti = new File("C:\\TheFourthGroupFile\\ContactInformation", +(s.length + 1) + ".txt");
                    content = s.length + 1;
                    filecontaintxti.createNewFile();
                    FileOutputStream fileOutputStream = new FileOutputStream(filecontaintxti);
                    byte[] bytes4 = new byte[100000000];
                    bytes4 = temp.getBytes();
                    fileOutputStream.write(bytes4);
                    System.out.println("是否还要输入(“否”“是”)");
                    String YN = scanner.nextLine();
                    if (YN.equals("否")) break;
                }
                System.out.println("姓名， 号码， QQ");
                for (int i = 0; i < content; i++) {
                    FileInputStream fileInputStream = new FileInputStream(new File("C:\\TheFourthGroupFile\\ContactInformation\\" + (i + 1) + ".txt"));
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    String leave;
                    while ((leave = reader.readLine()) != null) {
                        System.out.println(leave);
                    }
                }

            }
            File fil = new File("C:\\TheFourthGroupFile\\ContactInformation");
            String[] str = fil.list();
            if (choose.equals("删除")) {
                while (true) {
                    System.out.println("你要删除第几个工程师");
                    int howmany = scanner.nextInt();
                    for (int m = 0; m < str.length; m++) {
                        String temp = str[m];
                        str[m] = str[m].replace(".txt", "");
                        String hw = howmany + "";
                        if (hw.equals(sss)) {
                            File filedelet = new File("C:\\TheFourthGroupFile\\ContactInformation", temp);
                            filedelet.delete();
                            System.out.println("已经删除");
                        }
                    }


                    System.out.println("你是否还有删除“1还要删除”“2不删除”");
                    int howman = scanner.nextInt();
                    if (howman == 2) break;
                }


                File fil0 = new File("C:\\TheFourthGroupFile\\ContactInformation");
                String[] str0 = fil0.list();
                for (int m = 0; m < str0.length; m++) {
                    File file0 = new File("C:\\TheFourthGroupFile\\ContactInformation\\" + (m + 1) + ".txt");
                    if (file0.exists()) {
                        FileInputStream fileInputStream = new FileInputStream(file0);
                        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                        BufferedReader reader = new BufferedReader(inputStreamReader);
                        String leave;
                        while ((leave = reader.readLine()) != null) {
                            System.out.println(leave);
                        }
                    } else continue;


                }

            }

            System.out.println("你是否需要返回上一级(“返回”，“不返回”)");
            returnlayer = scanner.nextLine();
        }

        /*工程师查看留言*/
        protected void lookleaveword() throws EOFException {
            File filelookleave = new File("C:\\TheFourthGroupFile\\LeaveWord", "LeaveWords.txt");
            File fileloo = new File("C:\\TheFourthGroupFile\\LeaveWord");
            System.out.println("您将看到留言");
            if (!filelookleave.exists()) {
                System.out.println("还未有留言");
            }
            try {
                FileInputStream fileInputStream = new FileInputStream(filelookleave);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String leave;
                while ((leave = reader.readLine()) != null) {
                    System.out.println(leave);

                }

            } catch (IOException e) {
                e.getMessage();
            }
            System.out.println("你是否需要返回上一级(“返回”，“不返回”)");
            returnlayer = scanner.nextLine();
        }

        /* 工程师加入问题到数据库*/
        protected void Engineermap() throws IOException {
            int n = 1;
            while (true) {
                System.out.println("你要加入数据库的问题的的关键词");
                String HowMany = scanner.nextLine();
                File file = new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", HowMany + ".txt");
                try {
                    System.out.println("你的加入的答案是");
                    String Problem = scanner.nextLine();
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    byte[] bytes = new byte[100000000];
                    bytes = Problem.getBytes();
                    fileOutputStream.write(bytes);
                } catch (Exception e) {
                    e.getMessage();
                }
                System.out.println("你是否还要添加（输入：“是”“否”）");
                String s = scanner.nextLine();
                if (s.equals("否")) {
                    break;
                }
            }
            System.out.println("加入完毕,你是否需要返回上一级（如果输入“不返回”就结束程序）");
            returnlayer = scanner.nextLine();
        }
    }

    protected class User {
        /* 用户界面 用户选择要做是*/
        protected void userinterface() {
            System.out.println("请问您有什么需要帮助 (“查看工程师联系方式”或者“搜索问题”,“人工服务”,“留言”)（do you have question（Engineers and booking search problem））");
            userneedoperate = scanner.nextLine();
            if (userneedoperate.equals("搜索问题")) {
                System.out.println("你想搜索的问题是( do you want to search the problem)");
            } else if (userneedoperate.equals("查看工程师联系方式")) {
                System.out.println("你将查看工程师联系方式（you will make appointment engineer ）");
            } else if (userneedoperate.equals("人工服务")) {
                System.out.println("你将获得人工服务（you will make appointment engineer ）");
            } else if (userneedoperate.equals("留言")) {
                System.out.println("你将留言（you will make appointment engineer ）");
            } else while (true) {
                System.out.println("输入错误");
                System.out.println("请问您有什么需要帮助 (“查看工程师联系方式”或者“搜索问题”，“人工服务”，“留言”)（do you have question（Engineers and booking search problem））");
                userneedoperate = scanner.nextLine();
                if ((userneedoperate.equals("查看工程师联系方式")) || (userneedoperate.equals("搜索问题")) || (userneedoperate.equals("留言") || userneedoperate.equals("人工服务")))
                    break;


            }
        }

        /*人工服务*/
        protected void handleserivceEngineer() throws IOException {
            File filetxtcon = new File("C:\\TheFourthGroupFile\\ManualService\\", "ManualService.txt ");
            if (!filetxtcon.exists()) {
                filetxtcon.createNewFile();
            }
            System.out.println("请输入你要问的问题('输入完问题请回车输入“查看回答”')(“如果输入“结束”则停止服务)");
            while (true) {
                System.out.print("你的问题是： ");
                Userproblem = scanner.nextLine();
                if (Userproblem.equals("结束")) {
                    filetxtcon.delete();
                    break;
                }
                while (true) {
                    Date date = new Date();
                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String time = format.format(date);
                    Userproblem = time + ":  " + Userproblem;
                    FileOutputStream fileOutputStream = new FileOutputStream(filetxtcon);
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                    BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                    bufferedWriter.write(Userproblem);
                    bufferedWriter.flush();
                    String ss = scanner.nextLine();
                    if (ss.equals("查看回答")) break;
                }
                System.out.println();
                FileInputStream fileInputStream = new FileInputStream(filetxtcon);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String leave;
                while ((leave = reader.readLine()) != null) {
                    System.out.println("工程师的回答是：" + leave);
                }
            }

            System.out.println("你是否需要返回上一层（“是”，“否”）");
            userYN = scanner.nextLine();
        }

        /*用户留言*/
        protected void userleaveword() throws IOException {
            File fileleave = new File("C:\\TheFourthGroupFile", "LeaveWord");
            fileleave.mkdir();
            File fileleavetxt = new File("C:\\TheFourthGroupFile\\LeaveWord", "LeaveWords.txt");
            fileleavetxt.createNewFile();
            System.out.println("请留言(输入完请 回车 输入“提交”）");
            for (int i = 0; i < 1000; i++) {
                leaveword = scanner.nextLine();
                String ss = scanner.nextLine();
                Date date = new Date();
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = format.format(date);
                leaveword = time + ":  " + leaveword;
                FileOutputStream fileOutputStream = new FileOutputStream(fileleavetxt, true);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                bufferedWriter.write(leaveword);
                bufferedWriter.newLine();
                bufferedWriter.flush();
                if (ss.equals("提交")) break;
            }
            System.out.println("你是否需要返回上一层（“是”，“否”）");
            userYN = scanner.nextLine();
        }

        /*查看工程师的联系方式*/
        protected void userappointment() throws FileNotFoundException {
            File filecont = new File("C:\\TheFourthGroupFile\\ContactInformation");
            String[] ss = filecont.list();
            for (int i = 0; i < ss.length; i++) {
                FileInputStream fileInputStream = new FileInputStream(new File("C:\\TheFourthGroupFile\\ContactInformation\\" + (i + 1) + ".txt"));
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String leave;
                try {
                    while ((leave = reader.readLine()) != null) {
                        System.out.println(leave);
                    }
                } catch (IOException e) {
                    e.getMessage();
                }

            }
            System.out.println("希望对您有所帮助");
            System.out.println("你是否需要返回上一层（“是”，“否”）");
            userYN = scanner.nextLine();
        }

        /*搜索问题*/
        protected void Usermap() throws IOException {
            int i = 0;
            System.out.println("你有什么问题");
            String ss = scanner.nextLine();
            String[] folderName = fileanswer.list();
            for (i = 0; i < folderName.length; i++)/*存放文件名*/ {
                String temp = folderName[i];
                folderName[i] = folderName[i].replace(".txt", "");//去掉后缀
                //进行比对
                if (ss.indexOf(folderName[i]) >= 0) {
                    FileInputStream fileInputStream = new FileInputStream(new File("C:\\TheFourthGroupFile\\QuestionAnswerDatabase", temp));
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    String leave;
                    while ((leave = reader.readLine()) != null) {
                        System.out.println(leave);
                    }

                }
            }
            if (i == (ss.length())) {
                System.out.println("我们还不能解决您的问题，请您选择其他服务和留言");
            }

            System.out.println("你是否需要返回上一层（“是”，“否”）");
            userYN = scanner.nextLine();
        }

    }
}
